

clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user
 
set graphics on  

global path_data_processed "\data\processed"
global path_output "\data\output"

 
clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 
loc experiments "jobinsecurity jobinsecurity jobinsecurity jobinsecurity"
			   
gen posterior_educ = posterior*atleast_bachelor
gen tr_educ = treatment*atleast_bachelor

gen posterior_old = posterior*old
gen tr_old = treatment*old

gen posterior_female = 	posterior*female
gen tr_female = treatment*female

gen posterior_highearn = posterior*highearn
gen tr_highearn = treatment*highearn

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1

global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index
			   
preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

loc ++columns
qui eststo col`columns': reg x y

}

restore

loc colnum = 1
loc colnames ""

*** Column 1: Education

ivreg2 jobinsecurity (posterior posterior_educ = treatment tr_educ) $controls, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_educ, prec(3)
estadd loc thisstat5 = "`r(bstar)'": col`colnum'
estadd loc thisstat6 = "`r(sestar)'": col`colnum'

test posterior + posterior_educ = 0
estadd loc thisstat17 = string(r(p), "%9.3f"): col`colnum'

qui sum jobinsecurity
estadd loc thisstat19 = r(N): col`colnum'
*estadd loc thisstat20 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat21 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat22 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 2: Earnings

ivreg2 jobinsecurity (posterior posterior_highearn = treatment tr_highearn) $controls highearn, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_highearn, prec(3)
estadd loc thisstat8 = "`r(bstar)'": col`colnum'
estadd loc thisstat9 = "`r(sestar)'": col`colnum'

test posterior + posterior_highearn = 0
estadd loc thisstat17 = string(r(p), "%9.3f"): col`colnum'

qui sum jobinsecurity
estadd loc thisstat19 = r(N): col`colnum'
*estadd loc thisstat20 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat21 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat22 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 3: Age

ivreg2 jobinsecurity (posterior posterior_old = treatment tr_old) $controls old, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_old, prec(3)
estadd loc thisstat11 = "`r(bstar)'": col`colnum'
estadd loc thisstat12 = "`r(sestar)'": col`colnum'

test posterior + posterior_old = 0
estadd loc thisstat17 = string(r(p), "%9.3f"): col`colnum'

qui sum jobinsecurity
estadd loc thisstat19 = r(N): col`colnum'
*estadd loc thisstat20 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat21 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat22 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc ++colnum


*** Column 4: Gender

ivreg2 jobinsecurity (posterior posterior_female = treatment tr_female) $controls old, r first
mat firststage = e(first)
mat r2=e(r2)

sigstar posterior, prec(3)
estadd loc thisstat2 = "`r(bstar)'": col`colnum'
estadd loc thisstat3 = "`r(sestar)'": col`colnum'

sigstar posterior_female, prec(3)
estadd loc thisstat14 = "`r(bstar)'": col`colnum'
estadd loc thisstat15 = "`r(sestar)'": col`colnum'

test posterior + posterior_female = 0
estadd loc thisstat17 = string(r(p), "%9.3f"): col`colnum'

qui sum jobinsecurity
estadd loc thisstat19 = r(N): col`colnum'
*estadd loc thisstat20 = string(r2[1,1], "%9.2f"): col`colnum'	
estadd loc thisstat21 = string(firststage[8,1], "%9.2f"): col`colnum'	
estadd loc thisstat22 = string(firststage[8,2], "%9.2f"): col`colnum'	

loc rowlabels " " " "Posterior: Recession (a)" " " " " "Posterior: Recession $\times$ (b) " "High education" " " "Posterior: Recession $\times$ (b) " "High earnings" " " "Posterior: Recession $\times$ (b) " "Age$>$44" " " "Posterior: Recession $\times$ (b) " "Female" " " "Pr(a+b)=0" " " "Observations" " " "First stage F-stat (a)" "First stage F-stat (b)" " "   "\hline" " "   "

loc rowstats ""
loc rowstats ""

forval i = 1/22 {
loc rowstats "`rowstats' thisstat`i'"
}

esttab * using "jobsecurity_het_demographics_interact.tex", replace cells(none) booktabs nonotes nomtitles compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
mgroups("Personal unemployment (percent)"  , pattern(1 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

eststo clear	























